﻿using eDoc.BO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDoc.Controls.Blacklist
{
    public partial class ucAdd : System.Web.UI.UserControl
    {
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["id"]!=null)
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
            }
            if(!IsPostBack)
            {
                BindAppName();
                BindData();
            }
        }

        private void BindAppName()
        {
            DataTable dt = new DataTable();
            dt = BO.Data.Select_AppName_by_PartnerCode(Common.PartnerCode);
            ddlAppName.DataSource = dt;
            ddlAppName.DataBind();
            txtPartner.Text = Common.PartnerCode;
        }

        private void BindData()
        {
            if(id==0)
            {
                ltrTitle.Text = "Thêm mới blacklist";
                btAdd.Text = "Add";
                btDel.Visible = false;
                
            }
            else
            {
                DataTable dt = new DataTable();
                dt = BO.Data.Select_Blacklist(id);
                
                if(dt!=null && dt.Rows.Count>0)
                {
                    txtNote.Text = dt.Rows[0]["Note"].ToString();
                    txtUuid.Text = dt.Rows[0]["Uuid"].ToString();
                    ddlAppName.SelectedValue = dt.Rows[0]["AppID"].ToString();
                }

                ltrTitle.Text = "Cập nhật blacklist";
                btDel.Visible = true;
                btAdd.Visible = false;
                btAdd.Text = "Edit";
            }
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            int result = 0;

            if(string.IsNullOrEmpty(txtUuid.Text))
            {
                lblNote.Text = "Bạn chưa nhập Uuid!";
                txtUuid.Focus();
                return;
            }

            result = BO.Data.Insert_Blacklist(ddlAppName.SelectedValue, txtUuid.Text, txtNote.Text);

            if(result==1)
            {
                lblNote.Text = "Đã tồn tại bản ghi như vậy trong hệ thống!";
                txtUuid.Focus();
                return;
            }

            Response.Redirect("/blacklist.aspx");
        }

        protected void btDel_Click(object sender, EventArgs e)
        {
            BO.Data.Delete_Blacklist(id);
            Response.Redirect("/blacklist.aspx");
        }
    }
}